package cn.xiangxiang.service.impl;

import cn.xiangxiang.mapper.goods.BrandMapper;
import cn.xiangxiang.pojo.goods.Brand;
import cn.xiangxiang.service.goods.BrandService;
import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
public class BrandServiceImpl implements BrandService {

    @Autowired
    private BrandMapper brandMapper;


    /**
     * 查所有
     * @return
     */
    public List<Brand> findall() {
        return brandMapper.selectList(null);
    }


    public Page<Brand> findPage(Integer startpage, Integer pagesize) {
        Page<Brand> page = new Page<>(startpage, pagesize);
        Page<Brand> brandPage = brandMapper.selectPage(page, null);
        return brandPage;
    }


    public List<Brand> findCondition(Brand brand) {
        QueryWrapper<Brand> wrapper =  getWrapper(brand);
        List<Brand> brandList = brandMapper.selectList(wrapper);
        return brandList;
    }



    @Override
    public Page<Brand> findConditionPage(Integer startpage, Integer pagesize, Brand brand) {
        QueryWrapper<Brand> wrapper =  getWrapper(brand);
        Page<Brand> page = new Page<>(startpage, pagesize);
        return brandMapper.selectPage(page,wrapper);
    }

    @Override
    public Brand findById(Integer id) {
        return brandMapper.selectById(id);
    }

    @Override
    public void save(Brand brand) {
        /*此处，可以自己实现无法添加同品牌类目*/
        /*QueryWrapper<Brand> wrapper = getWrapper(brand);
        List<Brand> list = brandMapper.selectList(wrapper);
        if(list.size()>0){
            throw  new RuntimeException("该品牌已存在");
        }*/
        brandMapper.insert(brand);
    }

    @Override
    public void delById(Integer id) {
            brandMapper.deleteById(id);
    }

    @Override
    public void update(Brand brand) {
        brandMapper.updateById(brand);
    }

    private QueryWrapper<Brand> getWrapper(Brand brand) {
        QueryWrapper<Brand> wrapper = new QueryWrapper<>();
        if(brand!=null){
            if(brand.getName()!=null && !"".equals(brand.getName().trim())){
                wrapper.like("name",brand.getName());
            }
            if(brand.getLetter()!=null && !"".equals(brand.getLetter().trim())){
                wrapper.eq("letter",brand.getLetter());
            }
        }
        return wrapper;
    }
}
